using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using YushengMall.Backend.Api.Database;
using YushengMall.Backend.Api.Entity;
using YushengMall.Backend.Api.Repository;

namespace YushengMall.Backend.Api
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }
        readonly string _allowCors = "AllowSpecificOrigins";

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "YushengMall.Backend.Api", Version = "v1" });
            });

            services.AddCors(options =>
            {
                options.AddPolicy(_allowCors, builder =>
                {
                    builder.WithOrigins("http://localhost:8080", "*").AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin();
                });
            });
            var Ire = typeof(IRepository<>);
            var EfRe = typeof(EfRepository<>);

            services.AddScoped(Ire,EfRe);

            services.AddDbContext<DbHelper>(options =>
          {
              options.UseSqlServer();
              //options.UseNpgsql();
          });

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(option =>
            {
                option.RequireHttpsMetadata = false;
                option.SaveToken = true;
                var token = Configuration.GetSection("tokenParameter").Get<TokenParameter>();
                option.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(token.Secret)),
                    ValidIssuer = token.Issuer,
                    ValidateIssuer = true,
                    ValidateAudience = false
                };
            });

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "YushengMall.Backend.Api v1"));
            }

            //app.UseHttpsRedirection();
            //路由
            app.UseRouting();
            //token中间键
            app.UseAuthentication();
            app.UseAuthorization();

            //跨域入口
            app.UseCors(_allowCors);

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
